<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Fx_Resource
 *
 * @author anhdt
 */
class Fx_Resource {
    
    //constant
    const RES_TYPE_IMAGE = 'image';
    const RES_TYPE_VIDEO = 'video';
    const RES_TYPE_FLASH = 'flash';

    protected $_key = '';
    // file path
    protected $_file = '';
    // type of resource
    protected $_type = RES_TYPE_IMAGE;
    // meta data 
    protected $_metaData = array(
                                 'class'=>'',
                                 'id'=>'',
                                 'width'=>'',
                                 'height'=>'',
                                 'border'=>'',
                                 'title'=>''
                                 );
    // resource can be accessible
    protected $_accessible = TRUE;
    // resource file when to get
    protected $_resource_file = NULL;

    public function __construct($_key = '', $_file = '', $_type = 'image', $_accessible = TRUE) {
        if (!file_exists($_file)) {
            throw new Exception("File: $_file Not Found");
        }
        $this->setKey($_key);
        $this->setFile($_file);
        $this->setType($_type);
        $this->setAccessible($_accessible);
    }

    public function setKey($_key = '') {
        if (is_string($_key) && !empty($_key)) {
            $this->_key = $_key;
        } else {
            $this->_genKey();
        }
        return $this;
    }

    private function _genKey() {
        $this->_key = 'res_' . md5(time());
        return $this;
    }

    public function getKey() {
        if (empty($this->_key)) {
            $this->_genKey();
        }
        return (string)$this->_key;
    }

    public function setFile($_file) {
        if (is_string($_file)) {
            $this->_file = $_filepe;
        }
        return $this;
    }

    public function getFile() {
        if ($this->isAccessible()) {
            return (string)$this->_file;
        }
        return NULL;
    }

    public function setType($_type) {
        if (is_string($_type)) {
            switch ($_type) {
                case RES_TYPE_IMAGE:
                case RES_TYPE_VIDEO:
                case RES_TYPE_FLASH:
                    $this->_type = $_type;
                    break;
                default:
                    return $this;
                    break;
            }
        }
        return $this;
    }

    public function getType() {
        if ($this->isAccessible()) {
            return (string)$this->_type;
        }
        return NULL;
    }

    private function _setMetaData($_metaData = array()) {
        $this->_metaData = $_metaData;
        return $this;
    }

    public function getMetaData() {
        if ($this->isAccessible()) {
            return (array)$this->_metaData;
        }
        return NULL;
    }

    public function isAccessible() {
        return (boolean)$this->_accessible;
    }

    public function setAccessible($_accessible = TRUE) {
        $this->_accessible = $_accessible;
        return $this;
    }

    public function getOutputHtml() {
        if ($this->getType()) {
            $output   = '';
            $file     = $this->getFile();
            $type     = $this->getType();
            $metaData = $this->getOutputMetaData($type);
            switch ($type) {
                case self::RES_TYPE_IMAGE:
                    $output = sprintf("<img src=\"%s\" %s/>\n", $file, $metaData);
                    break;
                default:
                    $output = NULL;
                    break;
            }
            return $output;
        }
        return NULL;
    }

    public function getOutputMetaData($type = self::RES_TYPE_IMAGE) {
        $output = '';
        foreach ($this->_metaData as $key => $data) {
            $output = "$key=\"$data\" ";
        }
        return $output;
    }

    public function outputHtml() {
        echo $this->getOutputHtml();
        return $this;
    }

    public static function _isValidType($_type = '') {
        $_valid_types = array(self::RES_TYPE_IMAGE, self::RES_TYPE_FLASH, self::RES_TYPE_VIDEO);
        if (!in_array($_type, $_valid_types)) {
            return FALSE;
        }
        return TRUE;
    }
}
?>
